package com.gs.common

import android.app.Activity
import android.content.pm.ActivityInfo
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.core.widget.addTextChangedListener

open class BaseActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

    /**
     * 监听所有编辑框的内容是否填充，如果填充就改变button字体颜色以及背景
     * @param button 按钮
     * @param edts 编辑框数组，存储编辑框
     * @param areEdtsFilled 所有编辑框是否都被填充，这个数组的第一位数是表示按钮是否被激活的标志
     * @param formerBg 按钮原本的背景
     * @param formerTextColorId 按钮原本的字体颜色资源Id
     * @param changeBg 按钮改变后的背景
     * @param changeTextColorId 按钮改变后的字体颜色资源Id
     */
    protected fun activeButton(
        button: Button,
        edts: Array<EditText>,
        areEdtsFilled: Array<Boolean>,
        formerBg: Drawable,
        formerTextColorId: Int,
        changeBg: Drawable,
        changeTextColorId: Int
    ) {
        for ((index, item) in edts.withIndex()) {
            item.addTextChangedListener {
                areEdtsFilled[index + 1] = it!!.isNotEmpty()
                if (!areEdtsFilled[index + 1]) {
                    button.background = formerBg
                    button.setTextColor(getColor(formerTextColorId))
                    areEdtsFilled[0] = false
                } else {
                    for ((index, isFill) in areEdtsFilled.withIndex()) {
                        if (index != 0 && !isFill) {
                            button.background = formerBg
                            button.setTextColor(getColor(formerTextColorId))
                            areEdtsFilled[0] = false
                            return@addTextChangedListener
                        }
                    }
                    button.background = changeBg
                    button.setTextColor(getColor(changeTextColorId))
                    areEdtsFilled[0] = true
                }
            }
        }
    }

    protected fun dpToPx(dp: Int): Int {
        val density = resources.displayMetrics.density
        return Math.round((dp as Float * density) as Float)
    }
}

